package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.InterventionDao;
import com.itheima.pojo.Intervention;
import com.itheima.service.InterventionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = InterventionService.class)
@Transactional
public class InterventionServiceImpl implements InterventionService {

    @Autowired
    private InterventionDao interventionDao;

    @Override
    public List<Integer> findFoodIdsByOrderId(Integer orderId) {
        //根据orderId查询一个健康干预
        Intervention intervention = interventionDao.findById(orderId);
        return null;
    }

    @Override
    public void addAdvice(Intervention intervention, Integer[] foodIds, Integer[] sportIds) {
        interventionDao.adviseAdd(intervention);

        Integer interventionId = intervention.getId();

        setInterventionAndFood(interventionId,foodIds);

        setInterventionAndSport(interventionId,sportIds);

    }

    @Override
    public Intervention findByOrderId(Integer id) {
        return interventionDao.findByOrderId(id);
    }

    private void setInterventionAndSport(Integer interventionId, Integer[] sportIds) {
        if (sportIds!=null&&sportIds.length>0){
            for (Integer sportId : sportIds) {
                Map map = new HashMap<>();
                map.put("interventionId",interventionId);
                map.put("sportId",sportId);
                interventionDao.setInterventionAndSport(map);
            }
        }
    }

    private void setInterventionAndFood(Integer interventionId, Integer[] foodIds) {
        if (foodIds!=null&&foodIds.length>0){
            for (Integer foodId : foodIds) {
                Map map = new HashMap<>();
                map.put("interventionId",interventionId);
                map.put("foodId",foodId);
                interventionDao.setInterventionAndFood(map);
            }
        }
    }

}
